/*
 * Yekcomputer (r) CMS
 * Copyright (c) 2009, Yekcomputer. All rights reserved.
 * Author : Babak Taraghi 'babak.taraghi@gmail.com'
 * Code licensed under the BSD License.
 * version: 1.0
 */
var showHideChildren = function(ele,parentId){
	
	if(ele.style.backgroundImage.indexOf("minus.gif") != -1){
		bgImage = "url("+BASEURL+"files/cp/icons/plus.gif)";
	}else{
		bgImage = "url("+BASEURL+"files/cp/icons/minus.gif)";
	}
	
	ele.style.backgroundImage = bgImage;
	$("#children-"+parentId).toggle("slow");
};

var moveCat = function(what,catId,modelName){
	one.showLoading();
	action = (what=="up")?"moveCatUp":"moveCatDown";
	$.ajax({
		type: "GET",
		url: BASEURL+"cp/categories/"+action+"/"+modelName+"/"+catId+"/"+document.getElementById("language").value,
		cache: false,
		success: function(msg){
			one.hideLoading();
			result = msg.split("-|-");
			if(typeof result[1] != "undefined"){
				if(result[0] == "1")
					one.parseContentResponse(result[2]);
			}else
				alert("Whoops! Something went wrong.\n Please refresh the page and try again.");
		},
		error : function(){
			one.hideLoading();
			alert("Whoops! Something went wrong.\n Please refresh the page and try again.");
		}
	});
};

var editCat = function(catId){
	var newName = "";
	while(newName == "")
		newName = prompt("Enter a new name for this category to rename : ","");
	if(newName != null && newName != ""){
		one.showLoading();
		$.ajax({
			type: "GET",
			url: BASEURL+"cp/categories/setCatName/"+catId+"/"+newName+"/"+document.getElementById("language").value,
			cache: false,
			success: function(msg){
				one.hideLoading();
				result = msg.split("-");
				if(typeof result[1] != "undefined"){
					if(result[0] == "1")
						$("#title-"+catId).html(newName);
				}else
					alert("Whoops! Something went wrong.\n Please refresh the page and try again.");
			},
			error : function(){
				one.hideLoading();
				alert("Whoops! Something went wrong.\n Please refresh the page and try again.");
			}
		});
	}
};

var removeCat = function(catId){
	if(confirm("Every languages for this record will be removed.\n Are you sure you want remove this category ?")){
		one.showLoading();
		$.ajax({
			type: "GET",
			url: BASEURL+"cp/categories/removeCat/"+catId,
			cache: false,
			success: function(msg){
				one.hideLoading();
				var result = msg.split("-");
				if(typeof result[1] != "undefined"){
					if(result[0] == "1"){
						$("#container-"+catId).hide("slow");
					}else{
						alert(result[1]);
					}
				}else
					alert("Whoops! Something went wrong.\n Please refresh the page and try again.");
			},
			error : function(){
				one.hideLoading();
				alert("Whoops! Something went wrong.\n Please refresh the page and try again.");
			}
		});
	}
};

var getOuterHTML = function(object) {
	var element;
	if (!object) return null;
	element = document.createElement("div");
	element.appendChild(object.cloneNode(true));
	return element.innerHTML;
};

var addSubCat = function(parentId,modelName){
	var newName = "";
	while(newName == "")
		newName = prompt("Enter a name for the new sub category : ","");
	if(newName != null && newName != ""){
		one.showLoading();
		$.ajax({
			type: "GET",
			url: BASEURL+"cp/categories/addSubCat/"+parentId+"/"+newName+"/"+document.getElementById("language").value,
			cache: false,
			success: function(msg){
				one.hideLoading();
				var result = msg.split("-");
				if(typeof result[1] != "undefined"){
					if(result[0] != "0"){
						newItemHTML = "<li class='treeLi' id='container-"+result[0]+"'><a><span id='title-"+result[0]+"'>"+newName+"</span></a>";
						newItemHTML += "&nbsp;&nbsp;&nbsp;<a title='Rename Category' onclick='editCat("+result[0]+")'><img src='"+BASEURL+"/files/cp/icons/edit.gif' border='0'></a>";
						newItemHTML += "<a title='Add Sub Category' onclick='addSubCat("+result[0]+",\""+modelName+"\")'><img src='"+BASEURL+"/files/cp/icons/add.gif' border='0'></a>";
						newItemHTML += "<a title='Remove Category' onclick='removeCat("+result[0]+")'><img src='"+BASEURL+"/files/cp/icons/delete.gif' border='0'></a>";
						newItemHTML += "<a onclick='moveCat(\"up\","+result[0]+",\""+modelName+"\")'><img src='"+BASEURL+"/files/cp/icons/up.gif' border='0'></a>";
						newItemHTML += "<a onclick='removeCat(\"down\","+result[0]+",\""+modelName+"\")'><img src='"+BASEURL+"/files/cp/icons/down.gif' border='0'></a></li>";
						if(document.getElementById("children-"+parentId))
							$("#children-"+parentId).append(newItemHTML);
						else{
							container = document.getElementById("container-"+parentId);
							element = document.createElement("ul");
							element.setAttribute("id","children-"+parentId);
							element.innerHTML += newItemHTML;
							container.appendChild(element);
						}
					}
				}else
					alert("Whoops! Something went wrong.\n Please refresh the page and try again.");
			},
			error : function(msg){
				console.log(msg.response);
				one.hideLoading();
				alert("Whoops! Something went wrong.\n Please refresh the page and try again. ");
			}
		});
	}
};

var setCatsLanguage = function(lang,modelName){
	currentLangId = document.getElementById("language").value;
	document.getElementById(currentLangId).className = "languageTab";
	document.getElementById(lang).className = "currentLanguageTab";
	document.getElementById("language").value = lang;
	one.changeContent(BASEURL+"cp/categories/getCatsHTMLForLang/"+modelName+"/"+lang);
};